package com.mz.ozy.activity.me;


import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.mz.ozy.R;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.NewJoinMemListBean;
import com.mz.ozy.mvp.presenter.JoinMemberPresenter;
import com.mz.ozy.mvp.view.JoinMemberContract;
import com.mz.ozy.utils.KeyboardUtils;
import com.mz.ozy.utils.MD5Util;
import com.mz.ozy.utils.PayManager;
import com.mz.ozy.utils.ToastUtils;
import com.mz.ozy.widget.dialog.PayPassDialog;
import com.mz.ozy.widget.dialog.PayPassView;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class JoinMemberActivity extends BaseActivity<JoinMemberContract.View, JoinMemberPresenter> implements JoinMemberContract.View {

    public static final int PAY_TYPE_WX = 2;
    public static final int PAY_TYPE_ALIPAY = 1;
    public static final int PAY_TYPE_BONUS = 0;

    private OptionsPickerView optionView;
    private PayPassDialog dialog;

    @BindView(R.id.iv_wx_select)
    ImageView ivWxSelect;
    @BindView(R.id.iv_alipay_select)
    ImageView ivAlipaySelect;
    @BindView(R.id.iv_bank_select)
    ImageView ivBankSelect;

    @BindView(R.id.in_phone)
    EditText in_phone;
    @BindView(R.id.in_recommend_id)
    EditText in_recommend_id;
    @BindView(R.id.in_package)
    TextView in_package;

    @BindView(R.id.btn_comit)
    Button btnComit;

    private PayManager mPayManager;

    private int type = PAY_TYPE_ALIPAY;
    List<NewJoinMemListBean.DataDTO> options1Items = new ArrayList<>();
    NewJoinMemListBean.DataDTO selectModel = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_join_member);

        ButterKnife.bind(this);
        initView();
    }

    public void initView() {
        ButterKnife.bind(this);
        initTitle();
        tvTitle.setVisibility(View.GONE);
        tvCenterTitle.setText("购买会员");

        mPayManager = new PayManager(this);

        mPresenter.list(this);
    }

    @OnClick({R.id.ll_wx, R.id.ll_alipay, R.id.ll_source, R.id.select_package, R.id.btn_comit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_source:
                type = PAY_TYPE_BONUS;
                updateSelectStatus();
                break;
            case R.id.ll_wx:
                type = PAY_TYPE_WX;
                updateSelectStatus();
                break;
            case R.id.ll_alipay:
                type = PAY_TYPE_ALIPAY;
                updateSelectStatus();
                break;

            case  R.id.select_package:
                KeyboardUtils.hideKeyboard(in_phone);
                showAddressDialog();
                break;

            case R.id.btn_comit:

                String in_phone_str = in_phone.getText().toString();
                if (TextUtils.isEmpty(in_phone_str)){
                    ToastUtils.showToast(in_phone.getHint().toString());
                    return;
                }

                String in_recommend_id_str = in_recommend_id.getText().toString();
                if (TextUtils.isEmpty(in_recommend_id_str)){
                    ToastUtils.showToast(in_recommend_id.getHint().toString());
                    return;
                }

                if (selectModel == null){
                    ToastUtils.showToast(in_package.getHint().toString());
                    return;
                }

                if (type == PAY_TYPE_BONUS) {
                    dialog = new PayPassDialog(this);
                    dialog.getPayViewPass().setForgetText(selectModel.amount);
                    dialog.getPayViewPass().setHintText("确认购买会员");
                    dialog.getPayViewPass()
                            .setPayClickListener(new PayPassView.OnPayClickListener() {
                                @Override
                                public void onPassFinish(String passContent) {
                                    //6位输入完成回调
//                                mPresenter.onTb(MeWithActivity.this, type + "", accoun, money, MD5Util.getMD5String(passContent));
                                    //String amount, String name, String account, String type
                                    mPresenter.join(JoinMemberActivity.this, in_phone_str, in_recommend_id_str, selectModel.gift + "", type + "", MD5Util.getMD5String(passContent));
                                    dialog.dismiss();
                                }

                                @Override
                                public void onPayClose() {
                                    dialog.dismiss();
                                    //关闭回调
                                }

                                @Override
                                public void onPayForget() {
                                    dialog.dismiss();
                                    //点击忘记密码回调
                                    startActivity(new Intent(JoinMemberActivity.this, PayPwdActivity.class));
                                }
                            });
                } else {
                    mPresenter.join(JoinMemberActivity.this, in_phone_str, in_recommend_id_str, selectModel.gift + "", type + "", "");
                }
                break;

            default:

                break;
        }
    }

    private void updateSelectStatus() {
        ivWxSelect.setImageResource(R.mipmap.cb_red_normal);
        ivAlipaySelect.setImageResource(R.mipmap.cb_red_normal);
        ivBankSelect.setImageResource(R.mipmap.cb_red_normal);
        if (type == PAY_TYPE_WX) {
            ivWxSelect.setImageResource(R.mipmap.cb_red_select);
        } else if (type == PAY_TYPE_ALIPAY) {
            ivAlipaySelect.setImageResource(R.mipmap.cb_red_select);
        } else {
            ivBankSelect.setImageResource(R.mipmap.cb_red_select);
        }
    }

    private void showAddressDialog() {
        if (optionView == null) {
            optionView = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
                @Override
                public void onOptionsSelect(int options1, int option2, int options3, View v) {
                    if (options1Items != null && options1Items.size() > options1){
                        selectModel = options1Items.get(options1);
                        in_package.setText(selectModel.name);
                    }
                }
            })
                    .setSubmitText("确定")//确定按钮文字
                    .setCancelText("取消")//取消按钮文字
                    .setOutSideCancelable(true)
                    .build();
            optionView.setPicker(options1Items, null, null);
        }
        optionView.show();
    }

    @Override
    public void listSuccess(NewJoinMemListBean bean) {
        options1Items = bean.data;
    }

    @Override
    public void joinSuccess(JSONObject bean) {
        String respCode = bean.optString("respCode");
        String respMsg = bean.optString("respMsg");
        if ("00000".equalsIgnoreCase(respCode)){
            if (type == PAY_TYPE_ALIPAY) {
                mPayManager.handleAliPay(bean.optString("zfb"));
            } else if(type == PAY_TYPE_WX) {
                mPayManager.handleWxPay(bean);
            } else {
                ToastUtils.showToast("开通成功");
                finish();
            }

        } else {
            ToastUtils.showToast(respMsg != null ? respMsg : "网络请求失败，请稍后再试");
        }
    }

    @Override
    public void handReportSuccess(JSONObject bean) {

    }
}